<template>
  <div class="login_container">
    <div class="left">
      <div class="logo1">
        <img src="../assets/logo1.png" alt="" />
      </div>
    </div>
    <div class="right">
      <div class="logo2">
        <img src="../assets/logo2.png" alt="" />
        <div class="login_box">
          <!-- 登录表单区域 -->
          <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="0px" class="login_form">
            <!-- 用户名 -->
            <el-form-item prop="username"> <el-input v-model="loginForm.name" prefix-icon="iconfont icon-user" placeholder="用户名"></el-input> </el-form-item>
            <!-- 密码 -->
            <el-form-item prop="password">
              <el-input v-model="loginForm.pwd" prefix-icon="iconfont icon-3702mima" type="password" placeholder="密码"></el-input>
            </el-form-item>
            <!-- 按钮 -->
            <el-form-item class="btns">
              <el-button @click="login">登录</el-button>
              <!-- <el-button type="info" @click="resetLoginForm">重置</el-button> -->
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 登录表单的数据绑定对象
      loginForm: {
        name: '',
        pwd: ''
      },
      // 表单的验证规则对象
      loginFormRules: {
        // 验证用户名是否合法
        name: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        // 验证密码是否合法
        pwd: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 点击重置按钮，重置登录表单
    resetLoginForm() {
      // console.log(this);
      this.$refs.loginFormRef.resetFields()
    },
    login() {
      this.$refs.loginFormRef.validate(async (valid) => {
        // console.log(valid)
        if (!valid) return 0
        const res = await this.$http.post('auth/managerLogin', this.loginForm)
        console.log(res)
        if (res.data.code !== 200) return this.$message.error('登录失败！')
        this.$message.success('登录成功！')
        // console.log(res)
        window.sessionStorage.setItem('activePath', '/usermanager')
        window.sessionStorage.setItem('token', res.data.data.token)
        window.sessionStorage.setItem('userId', res.data.data.id)
        window.sessionStorage.setItem('userName', res.data.data.name)
        this.$router.push('/home')
        // console.log(res)
        // 1.将登录成功之后的 token，保存到客户端的 sessionStorage 中
        //   1.1项目中除了登录之外的其它api接口，必须在登录之后才能访问
        //   1.2token只应在当前网站打开期间生效，所以将 token 保存在sessionStorage 中
        // 2.通过编程式导航跳转到后台主页，路由地址是 /home
      })
    }
  }
}
</script>
<style>
body,
html,
#app {
  min-width: 1100px !important;
}
</style>
<style lang="less" scoped>
.login_container {
  width: 100%;
  height: 100%;
  // overflow-y: hidden;
  background: url('~@/assets/bgimg.png') no-repeat center center;
  background-size: 100% 100%;
  display: flex;
  justify-content: space-between;
  .left {
    width: 64%;
    height: 100%;
    position: relative;
    .logo1 {
      width: 54%;
      height: 112px;
      position: absolute;
      left: 20%;
      top: 0;
      bottom: 0;
      margin: auto;
      img {
        width: 100%;
        height: 100%;
      }
    }
  }
  .right {
    width: 35%;
    height: 100%;
    background: #000000;
    background-color: rgba(0, 0, 0, 0.3);
    position: relative;
    .logo2 {
      width: 83%;
      height: 78%;
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
      margin: auto;
      background: url('~@/assets/triggle.png') no-repeat center center;
      background-size: 100% 100%;
      img {
        width: 35%;
        // height: 37px;
        position: absolute;
        top: 30%;
        left: 8%;
      }
      .login_box {
        width: 60%;
        height: 200px;
        // background-color: #fff;
        border-radius: 3px;
        position: absolute;
        left: 33%;
        top: 55%;
        transform: translate(-50%, -50%);
      }
      .btns {
        display: flex;
        justify-content: flex-start;
        /deep/.el-button {
          width: 172px;
          height: 40px;
          background: #333333;
          box-shadow: 3px 3px 4px 0px rgba(141, 141, 141, 0.5);
          border-radius: 20px;
          span {
            font-size: 16px;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #ffffff;
          }
        }
      }
      .login_form {
        position: absolute;
        bottom: 0;
        width: 100%;
        padding: 0 20px;
        box-sizing: border-box;
      }
    }
  }
}
</style>
